#include "Project.h"
#include "LanguageXmlSerializer.h"
#include <QFile>
#include <QDebug>

Project::Project() :
    ProjectName( "" ),
    projectDir()
{
    language_map = new QMap<QString, LanguageModel *>();
}

Project::~Project()
{

}

void Project::addLanguage( QString language_name, LanguageModel * model )
{
    language_map->insert( language_name, model );
}

void Project::save( const QString & base_filename )
{
    QFile
        file( base_filename +  "_project.xml" );
    QXmlStreamWriter
        * writer;
    LanguageXmlSerializer
		language_serializer;
    
    if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
    {
        return;
    }
    
    writer = new QXmlStreamWriter( &file );

    writer->setAutoFormatting( true );

    writer->writeStartDocument();
    {
        writer->writeStartElement( "PinaLocadaProject" );
        {
            LanguageXmlSerializer
		        language_serializer;
            QString
                language_file_name;

            writer->writeTextElement( "ProjectName", ProjectName );

            QMapIterator<QString, LanguageModel *> iterator( *language_map );

            while ( iterator.hasNext() )
            {
                iterator.next();

                writer->writeTextElement( "Language", iterator.key() );

                QString language_file_name;

		        language_file_name = base_filename + "_language_" + iterator.key() + ".xml";
		        language_serializer.saveFile( language_file_name, iterator.value() );
            }
        }
        writer->writeEndElement();
    }
    writer->writeEndDocument();

    delete( writer );
}

bool Project::load( QFile & file )
{
    QString file_name = file.fileName();
    
    QFileInfo file_info( file_name );

    projectDir = file_info.dir().absolutePath();

    if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
    {
        return false;
    }
    
    streamReader = new QXmlStreamReader( &file );
    
    while (!streamReader->atEnd() )
    {
        switch( streamReader->readNext() )
        {
            case QXmlStreamReader::StartDocument:
                break;
            
            case QXmlStreamReader::StartElement:
            {
                if( streamReader->name() == "PinaLocadaProject" )
                {
                    handleElement();
                }
                else
                {
                    //TODO throw exception
                }
            }
            break;
                
            case QXmlStreamReader::Characters:
            {
                Q_ASSERT( streamReader->isWhitespace() );
            }
            break;
                
            case QXmlStreamReader::EndDocument:
                break;

            default:
                qDebug() << "Unexpected element " << streamReader->tokenString() << "\n";
                delete streamReader;
                return false;
        }
    }
    
    if ( streamReader->hasError() ) 
    {
        qDebug() << "Error in xml : " << streamReader->errorString();
        delete streamReader;
        return false;
    }

    return false;
}

void Project::handleElement()
{
    QString language_file_name;
    QXmlStreamReader::TokenType tokenType = streamReader->readNext();

    language_map->clear();
    
    while ( tokenType != QXmlStreamReader::EndElement )
    {
        switch( tokenType )
        {
            case QXmlStreamReader::StartElement:
            {
                if( streamReader->name() == "Language" )
                {
                    LanguageXmlSerializer serializer;

                    LanguageModel * model = new LanguageModel;

                    language_file_name = streamReader->readElementText();

                    serializer.loadFile( projectDir + "/" +ProjectName +"_language_" + language_file_name + ".xml", model );

                    language_map->insert( language_file_name, model );
                }
                else if( streamReader->name() == "ProjectName" )
                {
                    ProjectName = streamReader->readElementText();
                }
                else
                {
                    qDebug() << "Unsupported tag " << streamReader->name() << " in Label tag\n";
                }

                break;
            }
            
            case QXmlStreamReader::Characters:
            {
                Q_ASSERT( streamReader->isWhitespace() );
            }
            break;

            default:
                qDebug() << "Unexpected element " << streamReader->tokenType() << "\n";
                return;
        }
        
        tokenType = streamReader->readNext();
    }
}
